import { getCreditorData } from "@@/apis/users/creditor"
import { reactive, ref } from "vue"

export function useTable() {
  const tableData = ref<any[]>([])
  const searchParams = reactive({
    caseNo: ""
  })

  const currentPage = ref(1)
  const pageSize = ref(50)
  const total = ref(0)

  interface dataResponse {
    data?: {
      list: any[]
      total: number
    }
  }

  const fetchData = async () => {
    const params = {
      caseNo: searchParams.caseNo,
      page: currentPage.value,
      pageSize: pageSize.value
    }
    const res = await getCreditorData(params) as dataResponse
    tableData.value = res.data?.list || []
    total.value = res.data?.total || 0
  }

  const handlePageChange = (page: number) => {
    currentPage.value = page
    fetchData()
  }

  // 每页数量切换
  const handleSizeChange = (size: number) => {
    pageSize.value = size
    fetchData()
  }

  return {
    tableData,
    searchParams,
    currentPage,
    pageSize,
    total,
    fetchData,
    handlePageChange,
    handleSizeChange
  }
}
